#include "KeyboardSimulator.h"
#include <windows.h>
#include <QDebug>
KeyboardSimulator::KeyboardSimulator()
{

}

void KeyboardSimulator::keyDown(uint16_t vk_Code)
{
    keyboard_keyDown(vk_Code);
}

void KeyboardSimulator::keyUp(uint16_t vk_Code)
{
    keyboard_keyUp(vk_Code);
}

void KeyboardSimulator::keyPress(uint16_t vk_Code)
{
    keyboard_keyPress(vk_Code);
}

void keyboard_text(char* str)
{
    qDebug() << "here";
    size_t n = strlen(str);
    qDebug() << "strlen = " << n;
    LPINPUT inputs = new tagINPUT[n];
    for(size_t i = 0; i < n; i++)
    {
        inputs[i].type = INPUT_KEYBOARD;
        inputs[i].ki.wVk = 0; // KEYEVENTF_UNICODE 时必须是 0
        inputs[i].ki.dwFlags = KEYEVENTF_UNICODE;
        inputs[i].ki.time = 0;
        inputs[i].ki.dwExtraInfo = 0;
        inputs[i].ki.wScan = str[i];
    }
    ::SendInput(n, inputs, sizeof(tagINPUT));

    delete [] inputs;
}

void keyboard_text(QString str)
{
    int n = str.length();
    qDebug() << "strlen = " << n;
    LPINPUT inputs = new tagINPUT[n];
    for(int i = 0; i < n; i++)
    {
        inputs[i].type = INPUT_KEYBOARD;
        inputs[i].ki.wVk = 0; // KEYEVENTF_UNICODE 时必须是 0
        inputs[i].ki.dwFlags = KEYEVENTF_UNICODE;
        inputs[i].ki.time = 0;
        inputs[i].ki.dwExtraInfo = 0;
        inputs[i].ki.wScan = str.at(i).unicode();
    }
    ::SendInput(n, inputs, sizeof(tagINPUT));

    delete [] inputs;
}

void keyboard_keyDown(uint16_t vk_Code)
{
    //扩展键
    DWORD dwFlages = 0;
    switch(vk_Code)
    {
    case(VK_NUMLOCK):
    case(VK_CAPITAL):
    case(VK_SCROLL):
    case(VK_CONTROL):
    case(VK_LCONTROL):
    case(VK_RCONTROL):
    case(VK_SHIFT):
    case(VK_LSHIFT):
    case(VK_RSHIFT):
    case(VK_MENU):
    case(VK_LMENU):
    case(VK_RMENU):
        dwFlages |= KEYEVENTF_EXTENDEDKEY;
        break;
    default:
        break;
    }

    WORD wScan = MapVirtualKey(vk_Code, 0);
    INPUT Input;

    Input.type = INPUT_KEYBOARD;
    Input.ki.wVk = vk_Code;
    Input.ki.wScan = wScan;
    Input.ki.dwFlags = dwFlages;

    SendInput(1, &Input, sizeof(INPUT));
}

void keyboard_keyUp(uint16_t vk_Code)
{
    //扩展键
    DWORD dwFlages = 0;
    switch(vk_Code)
    {
    case(VK_NUMLOCK):
    case(VK_CAPITAL):
    case(VK_SCROLL):
    case(VK_CONTROL):
    case(VK_LCONTROL):
    case(VK_RCONTROL):
    case(VK_SHIFT):
    case(VK_LSHIFT):
    case(VK_RSHIFT):
    case(VK_MENU):
    case(VK_LMENU):
    case(VK_RMENU):
        dwFlages |= KEYEVENTF_EXTENDEDKEY;
        break;
    default:
        break;
    }

    WORD wScan = MapVirtualKey(vk_Code, 0);
    INPUT Input;

    Input.type = INPUT_KEYBOARD;
    Input.ki.wVk = vk_Code;
    Input.ki.wScan = wScan;
    Input.ki.dwFlags = dwFlages | KEYEVENTF_KEYUP;

    SendInput(1, &Input, sizeof(INPUT));
}

void keyboard_keyPress(uint16_t vk_Code)
{
    //扩展键
    DWORD dwFlages = 0;
    switch(vk_Code)
    {
    case(VK_NUMLOCK):
    case(VK_CAPITAL):
    case(VK_SCROLL):
    case(VK_CONTROL):
    case(VK_LCONTROL):
    case(VK_RCONTROL):
    case(VK_SHIFT):
    case(VK_LSHIFT):
    case(VK_RSHIFT):
    case(VK_MENU):
    case(VK_LMENU):
    case(VK_RMENU):
        dwFlages |= KEYEVENTF_EXTENDEDKEY;
        break;
    default:
        break;片段
    }

    WORD wScan = MapVirtualKey(vk_Code, 0);
    INPUT Input[2];
    Input[0].type = INPUT_KEYBOARD;
    Input[0].ki.wVk = vk_Code;
    Input[0].ki.wScan = wScan;
    Input[0].ki.dwFlags = dwFlages;

    Input[1].type = INPUT_KEYBOARD;
    Input[1].ki.wVk = vk_Code;
    Input[1].ki.wScan = wScan;
    Input[1].ki.dwFlags = dwFlages | KEYEVENTF_KEYUP;

    SendInput(2, Input, sizeof(INPUT));
}
